<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>react loadable 简单实现 | eric_cyr&#39;s blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="什么是react-loadablenpm desc：A higher order component for loading components with dynamic imports.一个异步加载component的高阶组件 组件拆分做路由拆分可以达到一定程度的性能优化，loadable本质上做的事组件拆分 基本用法123456789101112131415import Loadable f">
<meta property="og:type" content="article">
<meta property="og:title" content="react loadable 简单实现">
<meta property="og:url" content="http://yoursite.com/2020/06/14/react-loadable-%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="eric_cyr&#39;s blog">
<meta property="og:description" content="什么是react-loadablenpm desc：A higher order component for loading components with dynamic imports.一个异步加载component的高阶组件 组件拆分做路由拆分可以达到一定程度的性能优化，loadable本质上做的事组件拆分 基本用法123456789101112131415import Loadable f">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://thejameskyle.com/img/react-loadable-split-bundles.png">
<meta property="article:published_time" content="2020-06-14T13:47:53.000Z">
<meta property="article:modified_time" content="2020-06-14T13:48:00.134Z">
<meta property="article:author" content="eric_cyr">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://thejameskyle.com/img/react-loadable-split-bundles.png">
  
    <link rel="alternate" href="/atom.xml" title="eric_cyr&#39;s blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">eric_cyr&#39;s blog</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">分享是程序员的第一美德</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-react-loadable-简单实现" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/14/react-loadable-%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/" class="article-date">
  <time datetime="2020-06-14T13:47:53.000Z" itemprop="datePublished">2020-06-14</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      react loadable 简单实现
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="什么是react-loadable"><a href="#什么是react-loadable" class="headerlink" title="什么是react-loadable"></a>什么是react-loadable</h2><p>npm desc：<br>A higher order component for loading components with dynamic imports.<br>一个异步加载component的高阶组件</p>
<h2 id="组件拆分"><a href="#组件拆分" class="headerlink" title="组件拆分"></a>组件拆分</h2><p>做路由拆分可以达到一定程度的性能优化，loadable本质上做的事组件拆分<br><img src="http://thejameskyle.com/img/react-loadable-split-bundles.png" alt=""></p>
<h3 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Loadable <span class="keyword">from</span> <span class="string">'react-loadable'</span>;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">const</span> LoadableBar = Loadable(&#123;</span><br><span class="line">  loader: <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">import</span>(<span class="string">'./components/Bar'</span>),</span><br><span class="line">  loading() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Loading...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyComponent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">  render() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">LoadableBar</span>/&gt;</span></span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>##简易实现<br>基本思路，在组件和页面之间加一层代理，通过文件异步加载实现懒加载</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, &#123; Component &#125; <span class="keyword">from</span> <span class="string">"react"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> loadable = <span class="function">(<span class="params">&#123; loader, loading: Loading &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="class"><span class="keyword">class</span> <span class="title">loadableComponent</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">    state = &#123;</span><br><span class="line">      LoadedComponent: <span class="literal">null</span></span><br><span class="line">    &#125;;</span><br><span class="line">    componentDidMount() &#123;</span><br><span class="line">      <span class="comment">//loader是一个函数 执行import操作</span></span><br><span class="line">      loader().then(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(res.default);</span><br><span class="line">        <span class="keyword">this</span>.setState(&#123; <span class="attr">LoadedComponent</span>: res.default &#125;);</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    render() &#123;</span><br><span class="line">      <span class="keyword">const</span> &#123; LoadedComponent &#125; = <span class="keyword">this</span>.state;</span><br><span class="line">      <span class="keyword">return</span> LoadedComponent ? <span class="xml"><span class="tag">&lt;<span class="name">loadedComponent</span> /&gt;</span></span> : <span class="xml"><span class="tag">&lt;<span class="name">Loading</span> /&gt;</span></span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> loadable;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/14/react-loadable-%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0/" data-id="ckbq7rmlm0006ig1l6tp97nr4" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/06/14/%E9%BB%91%E5%AE%A2%E4%B8%8E%E7%94%BB%E5%AE%B6/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          黑客与画家
        
      </div>
    </a>
  
  
    <a href="/2020/06/01/%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B-curry%E5%8C%96/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">函数式编程---curry化</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">May 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/06/22/vue-%E7%BB%84%E4%BB%B6%E9%80%9A%E4%BF%A1/">vue 组件通信</a>
          </li>
        
          <li>
            <a href="/2020/06/14/njuoj-%E9%80%92%E5%BD%92%E6%9F%A5%E8%AF%A2%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/">njuoj 递归查询子字符串</a>
          </li>
        
          <li>
            <a href="/2020/06/14/foo-bar-%E7%9A%84%E6%95%85%E4%BA%8B/">foo bar 的故事</a>
          </li>
        
          <li>
            <a href="/2020/06/14/pwa/">pwa</a>
          </li>
        
          <li>
            <a href="/2020/06/14/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E6%80%BB%E7%BB%93/">背包问题总结</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 eric_cyr<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>